<?php
//
// +------------------------------------------------------------------------+
// | Gecko Framework                                                        |
// +------------------------------------------------------------------------+
//
require_once("Gecko/Form/Exception.php");
require_once("Gecko/Form/Field.php");
require_once("Zend/Registry.php" );
require_once("Gecko/Router.php" );

/**
 * Date Selector with Javascript picker
 *
 * @package Gecko.Form.Field;
 * @author Christopher Valderrama <valderrama.christopher@gmail.com>
 * @copyright Copyright (c) 2008
 * @version $Id$v1.0$ 2008
 * @access public
 **/
class Gecko_Form_Field_Date extends Gecko_Form_Field {
	/**
	 * The Date Format
	 *
	 * @see Zend_Date
	 * @var string
	 */
	private $format;

	/**
	 * The Date locale
	 *
	 * @see Zend_Locale
	 * @var string
	 */
	private $locale;

	/**
	 * Static var to indicate if JavaScript Picker code
	 * was included previously
	 *
	 * @var boolean
	 */
	private static $JSIncluded = false;

	/**
	 * Control type
	 *
	 * @var string
	 */
	protected $type = 'date';

	/**
	 * Creates a new DatePicker control
	 *
	 * @param string $name
	 * @param string $value
	 * @param array $params
	 */
	public function __construct( $name, $value = "", $params = array() ) {
		parent::__construct( $name, $value, $params );

		$this->format = ( isset( $params['format'] ) ? $params['format'] : "yyyy-mm-dd" );
		$this->locale = ( isset( $params['locale'] ) ? $params['locale'] : null );

		$this->addValidator( new Zend_Validate_Date( $this->format, $this->locale ) );
	}

	/**
	 * Sets the control Value
	 *
	 * @param string
	 */
	public function setValue( $value ) {
		$params = $this->getParams();

		if(empty($value) && ( isset( $params['allowEmpty'] ) && ( $params['allowEmpty'] == true ) ) ) {
			/**
			 * If a value is empty then Zend_Validator_Date
			 * shouldn't check the value, because it will
			 * fire a error, but only if allowed empty
			 **/
			$this->clearValidators();
		}

		parent::setValue( $value );
	}

	/**
	 * Returns the HTML Control
	 *
	 * @return string
	 */
	public function getControl() {
		$params = $this->getParams();
		$value = $this->getValue();
		$name = $this->getName();
		$vars = array();
		$template = Gecko_Router::LIBRARY_DIR . "/Assets/Form/Templates/Date.php";
		$scriptDir = Gecko_Router::ASSETS_DIR . "/JavaScriptHelpers/popcal2";

		$vars['size'] = strlen( $this->format );
		$vars['name'] = $name;
		$vars['value'] = $value;
		$vars['rawName'] = $name;
		$vars['popcalDir'] = $scriptDir;
		$vars['format'] = $this->format;
		$vars['lang'] = ( isset( $params['lang'] ) ? $params['lang'] : 'en' );
		$vars['past'] = ( isset( $params['enablePast'] ) && ( $params['enablePast'] == true ) ) ? '1' : '0';
		$vars['inRouter'] = Zend_Registry::isRegistered("base");
		$vars['JSIncluded'] = self::$JSIncluded;
		if(isset($params['fxFunction'])) {
			$vars['fxFunction'] = $params['fxFunction'];
		}

		$html = Gecko_Template::renderTemplate( $template, $vars, true );

		if( !self::$JSIncluded ) {
			self::$JSIncluded = true;
		}

		return $html;
	}
}